package com.jzy.bxb.app.server;

import com.jzy.bxb.app.manager.RedisManager;
import com.jzy.bxb.core.handler.HttpHandler;
import com.jzy.bxb.model.constant.AppConstant;
import com.jzy.bxb.model.constant.RedisKey;
import com.jzy.bxb.model.mongo.dao.UserDao;
import com.jzy.bxb.model.mongo.entity.User;

import redis.clients.jedis.Jedis;

/**
 * 权限验证
 *
 * @author JiangZhiYong
 * @date 2017-06-26 QQ:359135103
 */
public abstract class AuthHttpHandler extends HttpHandler {

	protected User user;

	/**
	 * 登录验证
	 */
	public boolean auth() {
		String sid = getString("sid");
		if (sid == null) {
			return false;
		}
		Jedis client = RedisManager.getInstance().getClient();
		String key = RedisKey.UserSession.getKey(sid);
		if (!client.exists(key)) {
			return false;
		}
		client.expire(key, AppConstant.SESSION_EXPIRE_TIME);
		client.close();
		user = UserDao.getBySid(sid);
		if (user == null) {
			return false;
		}
		return true;
	}

	public User getUser() {
		return user;
	}

}
